#!/usr/bin/env bash
set -euo pipefail
APPDIR="/opt/universal-official-website"

sudo -u webapp -H bash -lc "
  cd '$APPDIR'
  echo '== Git pull =='
  git pull --rebase

  echo '== Venv & deps =='
  source venv/bin/activate
  pip install -U pip wheel
  [ -f requirements.txt ] && pip install -r requirements.txt || true

  echo '== DB upgrade =='
  FLASK_APP=manage.py flask db upgrade
"

echo "== Restart services =="
sudo systemctl daemon-reload
sudo systemctl restart universal
sudo systemctl status universal -l

# 如有 Celery：
# systemctl restart universal-official-website-celery
# systemctl restart universal-official-website-beat

systemctl status universal --no-pager || true
journalctl -u universal -n 60 --no-pager | tail -n +1